<?php


namespace app\admin\controller;

use think\Db;

class Serviceapplymoney extends AdminControl
{
    /**
     * Notes: 服务人员申请提现页面
     * User: shen
     * Date: 2020/4/24
     * Time: 18:04
     * @return mixed]
     */
    public function apply()
    {
        $condition = array();
        if (trim(input('param.name'))) {
            $title_name = input('param.name');
            $condition['name'] = array('like', "%$title_name%");
        }
        if (trim(input('param.bankname'))) {
            $bankname = input('param.bankname');
            $condition['bank_name'] = array('like', "%$bankname%");
        }
        if (trim(input('param.card'))) {
            $card = input('param.card');
            $condition['card'] = array('eq', $card);
        }
        if (trim(input('param.mobile'))) {
            $mobile = input('param.mobile');
            $condition['mobile'] = array('like', "%$mobile%");
        }

        $condition['status'] = array('eq', 0);
        $ApplyMoney_model = model('ApplyMoney');
        $list = $ApplyMoney_model->getapplyList($condition, 10);
        $this->assign('list', $list);
        $this->assign('show_page', $ApplyMoney_model->page_info->render());
        $this->assign('name', trim(input('param.name', '')));
        $this->assign('bank_name', trim(input('param.bank_name', '')));
        $this->assign('card', trim(input('param.card', '')));
        $this->assign('mobile', trim(input('param.mobile', '')));
        $type = intval(input('param.type'));
        $this->assign('type', $type);
        $this->setAdminCurItem('apply');
        return $this->fetch('apply');
    }

    /**
     * Notes: 服务人员申请提现页面
     * User: shen
     * Date: 2020/4/24
     * Time: 18:04
     * @return mixed]
     */
    public function ok()
    {
        $condition = array();
        if (trim(input('param.name'))) {
            $title_name = input('param.name');
            $condition['name'] = array('like', "%$title_name%");
        }
        if (trim(input('param.bankname'))) {
            $bankname = input('param.bankname');
            $condition['bank_name'] = array('like', "%$bankname%");
        }
        if (trim(input('param.card'))) {
            $card = input('param.card');
            $condition['card'] = array('eq', $card);
        }
        if (trim(input('param.mobile'))) {
            $mobile = input('param.mobile');
            $condition['mobile'] = array('like', "%$mobile%");
        }

        $condition['status'] = array('eq', 1);
        $ApplyMoney_model = model('ApplyMoney');
        $list = $ApplyMoney_model->getapplyList($condition, 10);
        $this->assign('list', $list);
        $this->assign('show_page', $ApplyMoney_model->page_info->render());
        $this->assign('name', trim(input('param.name', '')));
        $this->assign('bank_name', trim(input('param.bank_name', '')));
        $this->assign('card', trim(input('param.card', '')));
        $this->assign('mobile', trim(input('param.mobile', '')));
        $type = intval(input('param.type'));
        $this->assign('type', $type);
        $this->setAdminCurItem('ok');
        return $this->fetch('ok');

    }

    /**
     * Notes: 服务人员申请提现页面
     * User: shen
     * Date: 2020/4/24
     * Time: 18:04
     * @return mixed]
     */
    public function no()
    {
        $condition = array();
        if (trim(input('param.name'))) {
            $title_name = input('param.name');
            $condition['name'] = array('like', "%$title_name%");
        }
        if (trim(input('param.bankname'))) {
            $bankname = input('param.bankname');
            $condition['bank_name'] = array('like', "%$bankname%");
        }
        if (trim(input('param.card'))) {
            $card = input('param.card');
            $condition['card'] = array('eq', $card);
        }
        if (trim(input('param.mobile'))) {
            $mobile = input('param.mobile');
            $condition['mobile'] = array('like', "%$mobile%");
        }

        $condition['status'] = array('eq', 2);
        $ApplyMoney_model = model('ApplyMoney');
        $list = $ApplyMoney_model->getapplyList($condition, 10);
        $this->assign('list', $list);
        $this->assign('show_page', $ApplyMoney_model->page_info->render());
        $this->assign('name', trim(input('param.name', '')));
        $this->assign('bank_name', trim(input('param.bank_name', '')));
        $this->assign('card', trim(input('param.card', '')));
        $this->assign('mobile', trim(input('param.mobile', '')));
        $type = intval(input('param.type'));
        $this->assign('type', $type);
        $this->setAdminCurItem('no');
        return $this->fetch('no');
    }

    /**
     * Notes:审核
     * User: shen
     * Date: 2020/4/25
     * Time: 8:48
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function status()
    {
        $id = intval(input('param.id'));
        $info = db('apply_money')->where('id', $id)->find();
        if (empty($info)) {
            dsLayerOpenSuccess('操作失败');
        }
        if (request()->post()) {
            $update_data = array();
            $update_data['status'] = intval(input('post.status'));
            if ($update_data['status'] == 2) {
                $update_data['not_content'] = input('post.no_content');
            }
            $update_data['utime'] = time();
            $update_data['admin_id'] = session('admin_id');
            $update_data['admin_name'] = session('admin_name');
            $state = db('apply_money')->where('id', input('id'))->data($update_data)->update();
            if ($update_data['status'] == 1) {
                #同意减服务人员金额 添加用户金额记录
                $info = db('apply_money')->where('id', $id)->find();
                $service_info = db('fuwu_personal')->where('id', $info['member_id'])->find();
                $total_money = $service_info['money'] - $info['money'];
                $update_service_data = [
                    'money' => $total_money,
                ];
                db('fuwu_personal')->where('id', $info['member_id'])->data($update_service_data)->update();

                $insert_money_log_data = [
                    'member_id' => $info['member_id'],
                    'type' => 1,
                    'name' => $service_info['name'],
                    'money' => $info['money'],
                    'surplus_money' => $total_money,
                    'money_type' => 1,
                    'addtime' => time(),
                    'desc' =>'服务人员提现',
                ];
                db('money_log')->data($insert_money_log_data)->insert();
            }
            if ($state) {
                $this->log('审核服务人员提现申请，编号' . $id . ',审核人账号：' . $update_data['admin_name']);
                dsLayerOpenSuccess('操作成功');
            } else {
                dsLayerOpenSuccess('操作失败');
            }
        }
        $this->assign('list', $info);
        return $this->fetch('status');
    }

    /**
     * 获取卖家栏目列表,针对控制器下的栏目
     */
    protected function getAdminItemList()
    {
        $menu_array = array(
            array('name' => 'apply', 'text' => '待审核', 'url' => url('Serviceapplymoney/apply'),
            ),
            array('name' => 'ok', 'text' => '审核通过', 'url' => url('Serviceapplymoney/ok'),
            ),
            array('name' => 'no', 'text' => '审核拒绝', 'url' => url('Serviceapplymoney/no'),
            ),
        );

        return $menu_array;
    }
}